{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantum Support Vector Machines\n",
    "\n",
    "In this notebook, we will use quantum circuits to compute a kernel that we will later use in a classical SVM. We will use Qiskit's implementation, that already encapsulates the kernel matrix computation and the SVM training."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from qiskit import Aer\n",
    "from qiskit.ml.datasets import breast_cancer\n",
    "from qiskit.circuit.library import ZZFeatureMap\n",
    "from qiskit.aqua import QuantumInstance\n",
    "from qiskit.aqua.algorithms import QSVM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will start with a very simple example that is clearly linearly separable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(2020)\n",
    "n = 30\n",
    "\n",
    "# Training examples for class 0\n",
    "\n",
    "mean = [0, 1]\n",
    "cov = [[0.1, 0], [0, 0.1]]\n",
    "\n",
    "A = (np.random.multivariate_normal(mean, cov, n))\n",
    "x_A, y_A = A.T\n",
    "plt.plot(x_A, y_A, 'ro');\n",
    "\n",
    "# Training examples for class 1\n",
    "\n",
    "mean = [1, 0]\n",
    "cov = [[0.1, 0], [0, 0.1]]\n",
    "\n",
    "B = (np.random.multivariate_normal(mean, cov, n))\n",
    "x_B, y_B = B.T\n",
    "plt.plot(x_B, y_B, 'bo');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To encode the data, we will use the ZZFeatureMap in dimension 2 with linear entaglement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_map = ZZFeatureMap(feature_dimension=2, reps=2, entanglement='linear')\n",
    "feature_map.draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After we have generated the training data and defined the feature map, we can now train the QSVM. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "training_input = np.append(A,B,axis =0)\n",
    "training_labels = np.array([0]*n+[1]*n) # Training labels are 0 and 1 \n",
    "\n",
    "backend = Aer.get_backend('statevector_simulator')\n",
    "quantum_instance = QuantumInstance(backend)\n",
    "qsvm = QSVM(feature_map, quantum_instance = quantum_instance)\n",
    "\n",
    "qsvm.train(training_input,training_labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To test the performance, we are going to generate test data according to the same distributions used for training."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "n = 10\n",
    "\n",
    "# Test examples for class 0\n",
    "\n",
    "mean = [0, 1]\n",
    "cov = [[0.1, 0], [0, 0.1]]\n",
    "\n",
    "C = (np.random.multivariate_normal(mean, cov, n))\n",
    "x_C, y_C = C.T\n",
    "plt.plot(x_C, y_C, 'ro');\n",
    "\n",
    "# Test examples for class 1\n",
    "\n",
    "mean = [1, 0]\n",
    "cov = [[0.05, 0], [0, 0.05]]\n",
    "\n",
    "D = (np.random.multivariate_normal(mean, cov, n))\n",
    "x_D, y_D = D.T\n",
    "plt.plot(x_D, y_D, 'bo');\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now use the trained QSVM to predict the class of the test examples. We can also compute the accuracy directly if we provide the labels. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Prediction for examples in test class 0\",qsvm.predict(C))\n",
    "print(\"Prediction for examples in test class 1\",qsvm.predict(D))\n",
    "\n",
    "test_input = np.append(C,D,axis =0)\n",
    "test_labels = np.array([0]*n+[1]*n)\n",
    "\n",
    "print(\"Accuracy\",qsvm.test(test_input,test_labels))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use datasets already included in Aqua"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_Total, training_input, test_input, class_labels = breast_cancer(\n",
    "    training_size=100,\n",
    "    test_size=10,\n",
    "    n=2,\n",
    "    plot_data=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We first use a simple feature map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_map = ZZFeatureMap(feature_dimension=2, reps=1, entanglement='linear')\n",
    "feature_map.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qsvm = QSVM(feature_map, training_input, test_input)\n",
    "result = qsvm.run(quantum_instance)\n",
    "\n",
    "print(\"Accuracy: \", result['testing_accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now use a more sophisticated feature map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_map = ZZFeatureMap(feature_dimension=2, reps=2, entanglement='linear')\n",
    "feature_map.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qsvm = QSVM(feature_map, training_input, test_input)\n",
    "result = qsvm.run(quantum_instance)\n",
    "\n",
    "print(\"Accuracy: \", result['testing_accuracy'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
